home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_02 / multinh3.cpp < prev    next >
C/C++ Source or Header  |  1992-01-18  |  2KB  |  78 lines

  1.                                  // Chapter 9 - Program 3
  2. #include <iostream.h>
  3.  
  4. class moving_van {
  5. protected:
  6.    float payload;
  7.    float weight;
  8.    float mpg;
  9. public:
  10.    void initialize(float pl, float gw, float in_mpg) {
  11.         payload = pl;
  12.         weight = gw;
  13.         mpg = in_mpg; };
  14.    float efficiency(void) {
  15.         return(payload / (payload + weight)); };
  16.    float cost_per_ton(float fuel_cost) {
  17.         return(fuel_cost / (payload / 2000.0)); };
  18. };
  19.  
  20.  
  21. class driver {
  22. protected:
  23.    float hourly_pay;
  24.    float weight;
  25. public:
  26.    void initialize(float pay, float in_weight) {
  27.          hourly_pay = pay;
  28.          weight = in_weight; };
  29.    float cost_per_mile(void) {return(hourly_pay / 55.0); } ;
  30.    float drivers_weight(void) {return(weight); };
  31. };
  32.  
  33.  
  34. class driven_truck : public moving_van, public driver {
  35. public:
  36.    void initialize_all(float pl, float gw, float in_mpg, float pay)
  37.         { payload = pl;
  38.           moving_van::weight = gw;
  39.           mpg = in_mpg;
  40.           hourly_pay = pay; };
  41.    float cost_per_full_day(float cost_of_gas) {
  42.           return(8.0 * hourly_pay +
  43.                 8.0 * cost_of_gas * 55.0 / mpg); };
  44.    float total_weight(void) {
  45.           return(moving_van::weight + driver::weight); };
  46. };
  47.  
  48.  
  49. main()
  50. {
  51. driven_truck chuck_ford;
  52.  
  53.    chuck_ford.initialize_all(20000.0, 12000.0, 5.2, 12.50);
  54.    chuck_ford.driver::initialize(15.50, 250.0);
  55.  
  56.    cout << "The efficiency of the Ford is " <<
  57.            chuck_ford.efficiency() << "\n";
  58.  
  59.    cout << "The cost per mile for Chuck to drive is " <<
  60.            chuck_ford.cost_per_mile() << "\n";
  61.  
  62.    cout << "The cost of Chuck driving the Ford for a day is " <<
  63.            chuck_ford.cost_per_full_day(1.129) << "\n";
  64.  
  65.    cout << "The total weight is " << chuck_ford.total_weight() <<
  66.            "\n";
  67. }
  68.  
  69.  
  70.  
  71.  
  72. // Result of execution
  73. //
  74. // The efficiency of the Ford is .625
  75. // The cost per mile for Chuck to drive is 0.227273
  76. // The cost of Chuck driving the Ford for a day is 195.530762
  77. // The total weight is 12250
  78.